package net.dgg.rz.api.production.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.rz.api.production.service.RecognitionService;
import net.dgg.rz.production.entity.LevelRule;
import net.dgg.rz.production.exception.ProductOrderExcption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 认款源数据接收controller
 * Created by zyou on 2019/3/26.
 */
@Controller
@RequestMapping("/api/rzsc/recognition")
public class RecognitionController extends DggBaseController {

    @Autowired
    private RecognitionService recognitionService;


    /**
     * <p>@Description 查询是否已完结</p>
     * <p>@author zyou</p>
     * <p>@Time 2019/2/26 12:00 创建时间</p>
     * <p>@return</p>
     */
    @RequestMapping(value = "/query_is_admissible", method = RequestMethod.POST )
    @ResponseBody
    public DggRestResponse queryIsAdmissible(@RequestParam Map map) {
        try {
            //首先查询是否是crm4.0老数据，如果是（没有查到生产单，就返回是老数据，告诉财务可以认款）
            int count = recognitionService.countOrderByOrderId(map);
            Map resMap = new HashMap();
            if(count==0){
                resMap = recognitionService.queryIsAdmissibleByOrderService(resMap, map);
            }else {
                List<Long> productOrderBusinessIds = recognitionService.queryIsAdmissible(map);
                resMap.put("isCrm4","no");
                resMap.put("data",productOrderBusinessIds);
            }
            return this.getSuccessResponse(resMap);
        }  catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("查询失败");
        }
    }
    /**
     * <p>@Description 接收认款数据</p>
     * <p>@author zyou</p>
     * <p>@Time 2019/2/26 12:00 创建时间</p>
     * <p>@return</p>
     */
    @RequestMapping(value = "/pay_product_record", method = RequestMethod.POST )
    @ResponseBody
    public DggRestResponse payProductRecord(@RequestParam Map map) {
        try {
            recognitionService.saveRecognitionDatas(map);
            return this.getSuccessResponse(null);
        }  catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("融资生产系统接收数据失败");
        }
    }

    /**
     * <p>@Description 接收内部下单数据</p>
     * <p>@author zyou</p>
     * <p>@Time 2019/2/26 12:00 创建时间</p>
     * <p>@return</p>
     */
    @RequestMapping(value = "/pay_order", method = RequestMethod.POST )
    @ResponseBody
    public DggRestResponse payOrder(@RequestParam Map params) {
        try {
            recognitionService.savePayOrderDatas(params);
            return this.getSuccessResponse(null);
        }  catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("融资生产系统接收数据失败");
        }
    }

    /**
     * <p>@Description 查询晋降级规则配置</p>
     * <p>@author zyou</p>
     * <p>@Time 2019/2/26 12:00 创建时间</p>
     * <p>@return</p>
     */
    @RequestMapping(value = "/get_level_rule", method = RequestMethod.POST )
    @ResponseBody
    public DggRestResponse getLevelRule(@RequestParam Map params) {
        try {
            List<LevelRule> levelRules = recognitionService.getLevelRule(params);
            return this.getSuccessResponse(levelRules);
        }  catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("获取失败");
        }
    }

}
